import { axiosInstance } from 'src/boot/axios'
import CommonConfig from 'src/assets/js/config'
const baseUrlWorkOrder = CommonConfig.planningUrl + '/v1'
const baseUrlTpm = CommonConfig.tpmUrl + '/v1'
const baseUrlUpms = CommonConfig.upmsUrl + '/v1'
const baseUrlProcess = CommonConfig.processUrl + '/v1'
// const baseUrlWorkOrder2 = CommonConfig.planningUrl + '/v3'
const baseUrlSchema = CommonConfig.schemaUrl
const datacenterUrl = CommonConfig.datacenterUrl + '/v1'
const machinestatusUrl = CommonConfig.machinestatusUrl + '/v1'
const planningUrl = CommonConfig.planningUrl
import qs from 'qs'

const qsConfig = {
  arrayFormat: 'repeat',
  skipNulls: true,
  format: 'RFC3986'
}

// 列举工单
export function getWorkOrders (params) {
  const newUrl = `${planningUrl}/v4/work_orders`
  // let reqData = {
  //   params,
  //   paramsSerializer: function (devicesIds) {
  //     return qs.stringify(devicesIds, qsConfig)
  //   }
  // }
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}
// 列举工单详情
export function getOrderDetail (groupId) {
  const newUrl = `${planningUrl}/v4/groups/${groupId}/work_orders/details`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res.data)
  })
}

// 工单换线
export function workOrderChangeover (groupId) {
  const newUrl = `${planningUrl}/v4/groups/${groupId}/work_orders:changeover`

  return axiosInstance.post(newUrl).then(resp => {
    return Promise.resolve(resp)
  })
}

// 工单开工
export function workOrderStart (groupId) {
  const newUrl = `${planningUrl}/v4/groups/${groupId}/work_orders:start`

  return axiosInstance.post(newUrl).then(resp => {
    return Promise.resolve(resp)
  })
}

// 根据设备id查车间id
export function getWorkShopId (ids) {
  const newUrl = `${baseUrlTpm}/devices/workshops/id:batchGet`

  let config = {
    params: {
      ids
    },
    paramsSerializer: function (ids) {
      return qs.stringify(ids, qsConfig)
    }
  }

  return axiosInstance.get(newUrl, config).then(resp => {
    return Promise.resolve(resp)
  })
}

// 通过车间ID查询启用班制
export function getShifts (id, withShiftItems = false) {
  const newUrl = `${baseUrlSchema}/v1/workshop/${id}/shifts/use?withShiftItems=${withShiftItems}`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}
export function getWorkOrder (workOrderId) {
  const newUrl = `${planningUrl}/v1/checkin/work_order/propose_time?workOrderId=${workOrderId}`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}
// 根据班制id与设备id查询操作人
export function getOperator (deviceId, shiftId) {
  const newUrl = `${baseUrlWorkOrder}/user_device_binding/operator`

  let reqData = {
    params: {
      deviceId: deviceId || undefined,
      shiftId: shiftId || undefined
    }
  }

  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 工单报工
export function workOrdercheckin (groupId, query) {
  const newUrl = `${planningUrl}/v4/groups/${groupId}/work_orders/checkin_logs/manual`
  return axiosInstance.post(newUrl, query).then(resp => {
    return Promise.resolve(resp)
  })
}

// 工单报检
export function workOrderTest (query) {
  const newUrl = `${baseUrlWorkOrder}/quality_control_orders`

  return axiosInstance.post(newUrl, query).then(resp => {
    return Promise.resolve(resp)
  })
}

// 工单转序/关闭
export function workOrderClose (groupId, comments) {
  // let query = {
  // }
  // if (comments !== undefined && comments !== null) {
  //   query['comments'] = comments
  // }
  const newUrl = `${planningUrl}/v4/groups/${groupId}/work_orders:close`

  return axiosInstance.post(newUrl, comments).then(resp => {
    return Promise.resolve(resp)
  })
}
// 撤回工单
export function workOrderWithdraw (groupId) {
  const newUrl = `${planningUrl}/v4/groups/${groupId}/work_orders:withdrawal`
  return axiosInstance.post(newUrl).then(resp => {
    return Promise.resolve(resp)
  })
}

// 获取不良原因组
export function getCauseType (causeType) {
  const newUrl = causeType === 3 ? `${machinestatusUrl}/stop_cause_type:selectBatch` : `${baseUrlSchema}/v1/cause_type`
  let reqData = {
    params: {
      causeType: causeType || undefined,
      isFilter: false
    }
  }

  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取不良原因
export function getCauseItem (causeGroupId) {
  const newUrl = `${baseUrlUpms}/cause_item`

  let reqData = {
    params: {
      causeGroupId: causeGroupId || undefined,
      showDisable: false
    }
  }

  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 工单填报不良
export function orderBadReport (query) {
  const newUrl = `${baseUrlWorkOrder}/quality_control_causes:batchCreate`

  return axiosInstance.post(newUrl, query).then(resp => {
    return Promise.resolve(resp)
  })
}

// 获取车间列表
export function getWorkShop () {
  const newUrl = `${baseUrlSchema}/v1/workshop_kv`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取车间在用设备
export function getMachine (workshopId) {
  const newUrl = `${baseUrlTpm}/workshops/${workshopId}/devices`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

// 工单转移
export function orderTransfer (workOrderId, query) {
  const newUrl = `${baseUrlWorkOrder}/work_orders/${workOrderId}:transfer`

  return axiosInstance.post(newUrl, query).then(resp => {
    return Promise.resolve(resp)
  })
}

// 强制完工
export function orderForceComplete (groupId) {
  const newUrl = `${planningUrl}/v4/groups/${groupId}/work_orders:force_complete`

  return axiosInstance.post(newUrl).then(resp => {
    return Promise.resolve(resp)
  })
}

// 修正产量
export function offsetCompleteCount (workOrderId, query) {
  const newUrl = `${baseUrlWorkOrder}/work_orders/${workOrderId}/complete_count`

  return axiosInstance.put(newUrl, query).then(resp => {
    return Promise.resolve(resp)
  })
}

// 修正工时
export function changeTime (groupId, query) {
  const newUrl = `${baseUrlWorkOrder}/work_orders/${groupId}/complete_time`

  return axiosInstance.put(newUrl, query).then(resp => {
    return Promise.resolve(resp)
  })
}

// 根据工单ID 获取报工班制列表
export function checkinShifts (workOrderId) {
  const newUrl = `${baseUrlWorkOrder}/work_orders/${workOrderId}/checkin_shifts`

  return axiosInstance.get(newUrl).then(resp => {
    return Promise.resolve(resp)
  })
}

// 根据工单ID 获取操作人
export function getOperators (workOrderId) {
  const newUrl = `${baseUrlWorkOrder}/work_orders/${workOrderId}/operators`

  return axiosInstance.get(newUrl).then(resp => {
    return Promise.resolve(resp)
  })
}

// 强制关闭
export function orderForceClose (groupId) {
  const newUrl = `${planningUrl}/v4/groups/${groupId}/work_orders:force_close`

  return axiosInstance.post(newUrl).then(resp => {
    return Promise.resolve(resp)
  })
}

// 列举报工记录
export function getCheckinLogs (workOrderId) {
  const newUrl = `${baseUrlWorkOrder}/work_orders/${workOrderId}/checkin_logs`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

// 列举报工记录
export function getOrderCheckinLogs (groupId) {
  const newUrl = `${baseUrlWorkOrder}/work_orders/checkin_logs/group_id/${groupId}`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res.data)
  })
}
// 列举质检记录
export function orderQualityLogs (id, inputType) {
  const newUrl = `${baseUrlWorkOrder}/work_orders/${id}/quality_control_causes`
  let reqData = {
    params: {
      inputType
    }
  }

  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 列举工单操作记录
export function orderLogs (workOrderId, pageNum, pageSize) {
  const newUrl = `${baseUrlWorkOrder}/work_orders/${workOrderId}/logs`

  let reqData = {
    params: {
      pageSize: pageSize || undefined,
      pageNum: pageNum || undefined
    }
  }

  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 根据工序信息Id获取加工程序列表
export function processList (id) {
  let reqData = {
    params: {
      processInfoId: id
    }
  }
  const newUrl = `${baseUrlProcess}/process_Program_list`

  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取工艺信息
export function processInfo (id) {
  let reqData = {
    params: {
      id: id
    }
  }
  const newUrl = `${baseUrlProcess}/process_info`

  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

export function getWorkingSteps (workingStepId) {
  const newUrl = `${baseUrlProcess}/working_steps/${workingStepId}`

  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取工艺信息
export function processFileList (id) {
  let reqData = {
    params: {
      processInfoId: id
    }
  }
  const newUrl = `${baseUrlProcess}/process_file_list`

  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取用户所在部门的用户
export function getDepartmentUsers (userId, includeSubordinate) {
  const newUrl = `${datacenterUrl}/users/${userId}/department/users`

  let reqData = {
    params: {
      includeSubordinate
    }
  }

  return axiosInstance.get(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 工单暂停
export function orderSuspend (groupId) {
  const newUrl = `${planningUrl}/v4/groups/${groupId}/work_orders:suspend`
  let reqData = {
    params: {
    }
  }
  return axiosInstance.post(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

// 工单恢复
export function orderRecover (groupId) {
  const newUrl = `${planningUrl}/v4/groups/${groupId}/work_orders:restore`
  let reqData = {
    params: {
    }
  }
  return axiosInstance.post(newUrl, reqData).then((res) => {
    return Promise.resolve(res)
  })
}
// 获取车间
export function getWorkshops () {
  const newUrl = `${baseUrlSchema}/v1/workshop_kv`
  const params = {
    withPlantName: false
  }

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve((res))
  })
}
export function getDeviceIds (params) {
  const newUrl = `${baseUrlTpm}/device/getDeviceIds`
  // const params = {
  //   level: 0,
  //   schemeId
  // }

  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve((res))
  })
}

// 列举超产工单
export function getOverproduceWorkOrders (data) {
  const newUrl = `${baseUrlWorkOrder}/overproduce_work_orders`
  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

// 列举超产有关联的工单
export function getOverproduceRelateWorkOrders (params) {
  const newUrl = `${planningUrl}/v4/overproduce/relate/work_orders`
  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}

// 填报超产工单
export function postOverproduceWorkOrders (overproduceWorkOrderId, data) {
  const newUrl = `${baseUrlWorkOrder}/overproduce_work_orders/${overproduceWorkOrderId}:fillIn`
  return axiosInstance.post(newUrl, data).then((res) => {
    return Promise.resolve(res)
  })
}

// 获取操作员
export function getCheckinOperators (workOrderId, shiftId) {
  const newUrl = `${baseUrlWorkOrder}/work_orders/${workOrderId}/shifts/${shiftId}/checkin_operators`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve(res)
  })
}

export function getShiftDate (workshopId) {
  const newUrl = `${baseUrlSchema}/v1/workshops/${workshopId}/current_time/shift_date`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}

export function isShowBtn () {
  const newUrl = `${baseUrlWorkOrder}/work_order_report_log/is_show_btn`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve((res.data))
  })
}

// 根据生产计划ID 获取生产计划
export function getWorkshopPlanning (id) {
  const newUrl = `${baseUrlWorkOrder}/workshop_plannings/${id}`
  return axiosInstance.get(newUrl).then((res) => {
    return Promise.resolve((res))
  })
}
// 获取工艺信息
export function getProcessList (productIds) {
  // const params = {
  //   productIds
  // }
  const newUrl = `${baseUrlProcess}/process_info_id_names:getByProductIds`

  return axiosInstance.post(newUrl, productIds).then((res) => {
    return Promise.resolve(res.data)
  })
}
// 获取工艺信息
export function repushData (ids) {
  // const params = {
  //   productIds
  // }
  const newUrl = `${baseUrlWorkOrder}/work_order_report_log/re_push_data`

  return axiosInstance.post(newUrl, ids).then((res) => {
    return Promise.resolve(res)
  })
}

export function getLogs (params) {
  const newUrl = `${baseUrlWorkOrder}/work_order_report_log/list_logs`
  let config = {
    params,
    paramsSerializer: function (ids) {
      return qs.stringify(ids, qsConfig)
    }
  }
  return axiosInstance.get(newUrl, config).then((res) => {
    return Promise.resolve((res.data))
  })
}
